package com.wx.server.handler;

import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
import com.foxinmy.weixin4j.mp.event.ScribeEventMessage;
import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.TextResponse;
import com.foxinmy.weixin4j.response.WeixinResponse;
import com.server.modules.sys.entity.qr_memeber.QrMember;
import com.server.modules.sys.service.member.MemberService;
import com.server.modules.sys.service.qr_memeber.QrMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.foxinmy.weixin4j.qy.chat.ChatEventType.subscribe;

/**
 * 处理关注消息
 *
 * @className SubscribeMessageHandler
 * @author jinyu(foxinmy@gmail.com)
 * @date 2015年12月3日
 * @since JDK 1.6
 */
@Component
public class SubscribeMessageHandler extends MessageHandlerAdapter<ScribeEventMessage> {
    @Autowired
    private MemberService memberService;
    @Autowired
    private QrMemberService qrMemberService;
     @Override
    public WeixinResponse doHandle0(WeixinRequest request, ScribeEventMessage message) {
         if(message.getEventType().equals(subscribe)) {
             QrMember qrMember = new QrMember();
             qrMember.setQrTicker(message.getTicket());
             qrMember.setCreateTime(message.getFormatCreateTime());
             qrMember.setToUsername(message.getToUserName());
             qrMember.setOpenId(message.getFromUserName());
             qrMemberService.save(qrMember);
             return new TextResponse("欢迎关注～" + message.getEventKey() + "--FromUser[" + message.getFromUserName() + "]---type[" + message.getEventType() + "]---" + message.toString());
         }
         else {
             QrMember qrMember = new QrMember();
             qrMember.setQrTicker(message.getTicket());
             qrMember.setCreateTime(message.getFormatCreateTime());
             qrMember.setToUsername(message.getToUserName());
             qrMember.setOpenId(message.getFromUserName());
             qrMemberService.save(qrMember);
             return new TextResponse(message.getEventKey() + "--FromUser[" + message.getFromUserName() + "]---type[" + message.getEventType() + "]---");
         }
    }
}
